package com.ksc.storekeeper.client.presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootPopupContentEvent;
import com.ksc.storekeeper.client.mvp.NameTokens;
import com.ksc.storekeeper.client.presenter.ProductDetailsPresenter.ProductDetailsProxy;
import com.ksc.storekeeper.client.view.ProductDetailsView;
import com.ksc.storekeeper.shared.dto.ProductDTO;
import com.ksc.storekeeper.shared.rpc.ProductServiceAsync;

public class ProductDetailsPresenter extends Presenter<ProductDetailsView, ProductDetailsProxy> {
	
	@Inject
	PlaceManager placeManager;
	
	@Inject
	ProductServiceAsync productServiceAsync;
	
	@Inject
	public ProductDetailsPresenter(EventBus eventBus, ProductDetailsView view, ProductDetailsProxy proxy) {
		super(eventBus, view, proxy);		
	}

	@ProxyStandard
	@NameToken(NameTokens.PRODUCT_DETAIL)
	public interface ProductDetailsProxy extends ProxyPlace<ProductDetailsPresenter>{
		
	}

	@Override
	protected void revealInParent() {		
		RevealRootPopupContentEvent.fire(this, this);
		
	}
	
	@Override
	public void onReveal(){
		super.onReveal();
		Long id=Long.valueOf(placeManager.getCurrentPlaceRequest().getParameter("id", "0"));
		if(id!=0){
			productServiceAsync.getProductById(id, new AsyncCallback<ProductDTO>() {
				
				@Override
				public void onSuccess(ProductDTO result) {
					getView().displayProduct(result);
				}
				
				@Override
				public void onFailure(Throwable caught) {
					
				}
			});
		}
		
	}
}
